package ejb;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import utils.AbstractBean;
import dto.CandidateDTO;
import dto.MemberDTO;
import entitymanagers.MemberManager;
import model.Member;
import model.Party;

@Stateless
@LocalBean
public class MemberManagerBean extends AbstractBean<Member> implements MemberManager{

	@PersistenceContext
	private EntityManager em;
	
	public MemberManagerBean() {
		super(Member.class);
	}

	@Override
	public MemberDTO[] getAllMembersAllParties() {
		try{
			return convertToDTO(findAll());
		}
		catch (Exception e){
			return new MemberDTO[0];
		}
	}

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	public static MemberDTO convertToDTO(Member member) {
		MemberDTO memberDTO = new MemberDTO();
		memberDTO.setCitizen(memberDTO.getCitizen());
		memberDTO.setIdMember(memberDTO.getIdMember());
		memberDTO.setIdParty(memberDTO.getIdParty());
		return memberDTO;
	}
	private MemberDTO[] convertToDTO(List<Member> findAll) {
		if(findAll != null){
			MemberDTO[] allMembers = new MemberDTO[findAll.size()];
			int i = 0;
			for(Member m: findAll){
				allMembers[i] = convertToDTO(m);
				i++;
			}
			return allMembers;
		}
		return null;
	}

	@Override
	public void voteSubscriberStatus(MemberDTO member, CandidateDTO candidate) {
		// TODO Auto-generated method stub
		
	}
}
